package com.stars.home.fragment

import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.RecyclerView
import com.hwangjr.rxbus.annotation.Subscribe
import com.hwangjr.rxbus.annotation.Tag
import com.qmuiteam.qmui.kotlin.onClick
import com.stars.home.R
import com.stars.home.activity.CertificationActivity
import com.stars.home.activity.MainActivity
import com.stars.home.activity.RealNameActivity
import com.stars.home.adapter.HomeFristAdapter
import com.stars.home.base.MBRFragment
import com.stars.home.config.ConfigRx
import com.stars.home.config.MIntentAction
import com.stars.home.databinding.FragmentHomeFirstBinding
import com.stars.home.inte.ChoiceData
import com.stars.home.inte.ChoiceLocation
import com.stars.home.utils.CurrentLocationUtils
import com.stars.home.utils.CurrentOneLocationUtils
import com.stars.home.utils.PopuSwitchUtils
import com.stars.home.vmodel.HomeFirstVModel
import com.tencent.mmkv.MMKV
import com.trailer.base.net.IResp
import com.trailer.base.utils.B
import com.trailer.base.utils.UiSwitch

/**
 *on 2022/9/22
 */
class HomeFragment : MBRFragment<HomeFirstVModel, HomeFristAdapter, FragmentHomeFirstBinding>() {

    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)

        vm.getUserInfo()
        binding.refreshLayout.isEnabled = false
        vm.user.observe(this) {

            if (it.type == 0 && it.status == 0) {
                MMKV.defaultMMKV().putString("isRecord","0")// 未实名认证

                binding.llCertification.visibility = View.VISIBLE
                binding.llCertificationTo.visibility=View.GONE

            }
            if (it.type == 0 && it.status == 1) {
                MMKV.defaultMMKV().putString("isRecord","1")// 实名认证审核中

                binding.llCertification.visibility = View.GONE
                binding.tvCertification.text="实名认证信息审核中暂时不能接单！请等待"
                binding.llCertificationTo.visibility=View.VISIBLE
            }
            if (it.type == 0 && it.status == -1) {
                MMKV.defaultMMKV().putString("isRecord","-1")// 实名认证不通过

                binding.llCertification.visibility = View.GONE
                binding.tvCertification.text="实名认证信息审核不通过！"
                binding.llCertificationTo.visibility=View.VISIBLE
            }

            if(it.status==2){

                binding.llCertification.visibility = View.GONE
                binding.llCertificationTo.visibility=View.GONE
            }

            if(it.status==2&& it.isOnline==0){
                //弹框-去开启

                MMKV.defaultMMKV().putString("isRecord","3")// 未开启接单按钮

                PopuSwitchUtils.show(context(),binding.refreshTo,object : ChoiceData{
                    override fun onRestuse(id: String?) {
                        vm.setStatus()
                    }

                })

            }

            if(it.status==2&& it.isOnline==1){
                MMKV.defaultMMKV().putString("isRecord","4")// 已开启接单按钮

            }


        }

        vm.index.observe(this){

            try {
                adapter().removeAt(it)
            }catch (e:Exception){

            }

        }
        //获取当前城市

        lifecycleScope.launchWhenCreated {
            CurrentOneLocationUtils.getStartLocal(context() as MainActivity, object : ChoiceLocation {
                override fun onRestuse(lat: String, lng: String, city: String) {
                   binding.tvCityName.text=city
                }
            })
        }


    }

    override fun listeners() {
        super.listeners()

        binding.butCertification.onClick {
//            UiSwitch.singleRes(this@HomeFragment, CertificationActivity::class.java,
//                MIntentAction.REQUEST_CODE_ONE)

          UiSwitch.bundle(context, CertificationActivity::class.java, B().putString("type","1").ok())

        }

        binding.llCertificationTo.onClick {
            UiSwitch.single(context, RealNameActivity::class.java)
        }

        binding.recyclerView!!.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                val topRowVerticalPosition =
                    if (recyclerView == null || recyclerView.childCount == 0) 0 else recyclerView.getChildAt(
                        0
                    ).top
                binding.refreshTo!!.isEnabled =
                    topRowVerticalPosition >= 0 && recyclerView != null && !recyclerView.canScrollVertically(
                        -1
                    )
            }

            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
            }
        })


        //设置刷新监听器
        binding.refreshTo.setColorSchemeResources(
            R.color.refresh_colorPrimary,
            R.color.refresh_colorAccent,
            R.color.refresh_colorPrimaryDark
        )
        binding.refreshTo.setOnRefreshListener {

            vm.getUserInfo()
            onRefresh()
        }

        binding.llAll.onClick {
            binding.tvAll.textSize=14f
            binding.tvAll.setTextColor(Color.parseColor("#333333"))
            binding.lineAll.visibility=View.VISIBLE
            binding.tvDistance.textSize=12f
            binding.tvDistance.setTextColor(Color.parseColor("#999999"))
            binding.linDistance.visibility=View.INVISIBLE
            binding.tvPrice.textSize=12f
            binding.tvPrice.setTextColor(Color.parseColor("#999999"))
            binding.linePrice.visibility=View.INVISIBLE
            vm.moneySort="0"
            vm.distanceSort="0"
            adapter().clearList()
            onRefresh()


        }

        binding.llDistance.onClick {

            binding.tvAll.textSize=12f
            binding.tvAll.setTextColor(Color.parseColor("#999999"))
            binding.lineAll.visibility=View.INVISIBLE
            binding.tvDistance.textSize=14f
            binding.tvDistance.setTextColor(Color.parseColor("#333333"))
            binding.linDistance.visibility=View.VISIBLE
            binding.tvPrice.textSize=12f
            binding.tvPrice.setTextColor(Color.parseColor("#999999"))
            binding.linePrice.visibility=View.INVISIBLE
            vm.moneySort="0"
            vm.distanceSort="1"
            adapter().clearList()
            onRefresh()

        }

        binding.llPrice.onClick {

            binding.tvAll.textSize=12f
            binding.tvAll.setTextColor(Color.parseColor("#999999"))
            binding.lineAll.visibility=View.INVISIBLE
            binding.tvDistance.textSize=12f
            binding.tvDistance.setTextColor(Color.parseColor("#999999"))
            binding.linDistance.visibility=View.INVISIBLE
            binding.tvPrice.textSize=14f
            binding.tvPrice.setTextColor(Color.parseColor("#333333"))
            binding.linePrice.visibility=View.VISIBLE
            vm.moneySort="1"
            vm.distanceSort="0"
            adapter().clearList()
            onRefresh()

        }





    }

    override fun onListReceive(res: IResp<*>) {
        super.onListReceive(res)
        binding.refreshTo.isRefreshing = false
    }


//    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
//        super.onActivityResult(requestCode, resultCode, data)
//        if (MIntentAction.REQUEST_CODE_ONE === requestCode && resultCode == Activity.RESULT_OK) {
//            //认证成功
//            binding.llCertification.visibility = View.GONE
//            binding.llCertificationTo.visibility=View.VISIBLE
//        }
//
//    }


    override fun vmClass(): Class<HomeFirstVModel> {
        return HomeFirstVModel::class.java
    }

    override fun binding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentHomeFirstBinding {
        return FragmentHomeFirstBinding.inflate(layoutInflater)
    }

    override fun initAdapter(): HomeFristAdapter {
        return HomeFristAdapter(vm)
    }

    override fun title(): String? {
        return null
    }

    override val isRxbus: Boolean
        get() = true

    @Subscribe(tags = [Tag(ConfigRx.REFUSE_ORDER)])
    fun setRefuseOrder(position: String) {
        onRefresh()
    }
    @Subscribe(tags = [Tag(ConfigRx.TAKE_ORDER)])
    fun setTakeOrder(position: String) {
        onRefresh()
    }

    @Subscribe(tags = [Tag(ConfigRx.CERTIFICATION_TO)])
    fun setCertificationTo(type: String) {

        if(type=="1") {
            vm.getUserInfo()
        }

//        binding.llCertification.visibility = View.GONE
//        binding.llCertificationTo.visibility=View.VISIBLE

    }


}
